<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //91.92%
    var findShortestSubArray = function(nums) {
        const len = nums.length;
        let temp = [];  
        let firstIndex = {},lastIndex = {};
        let max = 0;
        for(let i=0; i<len; i++){
            let k = nums[i];
            temp[k] = (temp[k] || 0) + 1;
            max = Math.max(max, temp[k]);
            if(firstIndex[k] === undefined){
                firstIndex[k] = i;
            }
            lastIndex[k] = i;
        }
        let res = len;
        for(let k in temp){
            if(temp[k] == max){
                res = Math.min(res, lastIndex[k] - firstIndex[k] + 1);
            }
        }
        return res;
    };
    let nums = [1,3,2,2,3,1];
    console.log(findShortestSubArray(nums));
    
</script>
</html>